<template>
  <div>
    <avue-form ref="form" v-model="form" :option="option"></avue-form>
    <div class="bottom-menu" v-if="!isView">
      <el-button type="primary" size="small" :disabled="btn" @click="handelSubmit" :loading="isSubmit">保 存</el-button>
      <el-button size="small" @click="handleCancle" :loading="isSubmit">取 消</el-button>
    </div>
  </div>
</template>

<script>
  import {fetchList, addObj, putObj} from '@/api/subwaystm/swstinputpassengerwork/free-work'
  import {validateDeviceOperateNo} from "@/util/validate";
  export default {
    name: 'free-work',
    inject: ['passengerWork'],
    data() {
      return {
        form: {},
        isSubmit: false,
        relationId: null,
        btn: false,
        option: {
          labelWidth: 120,
          menuBtn: false,
          column: [{
            label: "闸门被误用张数",
            prop: 'gateMisuseNumber',
            type: 'number',
            // rules:[{
            //   required:true,
            //   message:"请输入闸门被误用张数",
            //   trigger:"blur"
            // }]
          },{
            label: "手机无电张数",
            prop: 'mobileNoeleNumber',
            type: 'number'
          },{
            label: "车票非人损张数",
            prop: "ticketDamageNumber",
            type: 'number',
            // rules:[{
            //   required:true,
            //   message:"请输入车票非人损张数",
            //   trigger:"blur"
            // }]
          },{
            label: "其它张数",
            prop: "otherNumber",
            type: 'number',
            // rules:[{
            //   required:true,
            //   message:"请输入其它张数",
            //   trigger:"blur"
            // }]
          },{
            label: "合计",
            prop: "total",
            type: 'number',
            disabled: true
          }
          // , {
          //   label:'上传截图',
          //   prop:'filePath',
          //   type: "upload",
          //   listType: 'picture-img',
          //   component: 'zd-upload'
          // }
          ]
        }
      }
    },
    watch: {
      form: {
        handler(){
          this.form.total = (this.form.gateMisuseNumber || 0) + (this.form.mobileNoeleNumber || 0) + (this.form.ticketDamageNumber
            || 0) + (this.form.otherNumber || 0)
        },
        deep: true
      }
    },
    computed: {
      isView(){
        return this.boxType == 'view'
      }
    },
    props: {
      workInfo: Object,
      boxType: String
    },
    methods: {
      getList(){
        if(this.relationId != this.workInfo.id){
          fetchList(this.workInfo.id).then(response => {
            this.relationId = this.workInfo.id
            this.form = response.data.data || {}
          })
        }
      },
      clear(){
        this.form = {}
      },
      handelSubmit(){
        if(this.form.gateMisuseNumber==null && this.form.mobileNoeleNumber==null && this.form.ticketDamageNumber==null && this.form.otherNumber==null){
          this.$message.info("闸门被误用张数、手机无电张数、车票非人损张数、其它张数至少填写一项！");
          return
        }
        this.form.total = (this.form.gateMisuseNumber || 0) + (this.form.mobileNoeleNumber || 0) + (this.form.ticketDamageNumber
          || 0) + (this.form.otherNumber || 0)

        var params = {
          ...this.workInfo,
          workList: [this.form]
        }
        if(this.boxType == 'add'){
          this.passengerWork.validateForm((valid)=>{
            if(valid){
              if (!validateDeviceOperateNo(this.workInfo.deviceOperateNum)) {
                this.$message.error("设备操作号必须为09开头的6位纯数字！");
                return;
              }
              this.$refs.form.validate(valid => {
                if(valid){
                  this.isSubmit = true
                  addObj(params).then(response => {
                    this.$message.success('添加成功')
                    this.isSubmit = false
                    this.btn = true
		    this.passengerWork.getList(this.passengerWork.page)//20240827 测试  点击保存成功后刷新主页面
                  })
                }
              })
            }
          })
        }else{
          this.passengerWork.validateForm((valid)=>{
            if(valid){
              this.$refs.form.validate(valid => {
                if(valid){
                  this.isSubmit = true
                  putObj(params).then(response => {
                    this.$message.success('修改成功')
                    this.isSubmit = false
                    this.btn = true
                  })
                }
              })
            }
          })
        }
      },
      handleCancle(){
        if(this.boxType == 'mergeEdit'){
          this.passengerWork.closeDialogMerge()
        }else{
          this.passengerWork.closeDialog()
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  .bottom-menu{
    margin-top: 30px;
    display: flex;
    justify-content: center
  }
</style>
